package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /*
     * 添加购物车
     * */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 判断购物车表中是否有要添加的数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.getShoppingCart(shoppingCart);

        if (list != null && list.size() > 0) {
            // 购物车表里有数据
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumber(cart);
        } else {
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                // 添加的是菜品
                DishVO dish = dishMapper.getDishById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                // 添加的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                SetmealVO setmealVO = setmealMapper.selectById(setmealId);
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 向购物车表插入新数据
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /*
    * 查看购物车
    * */
    public List<ShoppingCart> getShoppingCartList() {
        List<ShoppingCart> list = shoppingCartMapper.selectShoppingCartByUserId(BaseContext.getCurrentId());

        return list;
    }

    /*
    * 删除购物车中一个商品
    * */
    public void reduceShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.getShoppingCart(shoppingCart);
        if (list == null || list.isEmpty()) throw new ShoppingCartBusinessException("此货物不存在");
        ShoppingCart cart = list.get(0);
        if (cart.getNumber() == 1){
            // 数量只剩一个 ，删除这条数据
            shoppingCartMapper.deleteById(cart.getId());
        }else{
            cart.setNumber(cart.getNumber() - 1);
            // 数量大于1 修改这条数据
            shoppingCartMapper.updateNumber(cart);
        }
    }
    /*
    * 清空购物车
    * */
    public void clearShoppingCart() {
        shoppingCartMapper.clearShoppingCartByUserId(BaseContext.getCurrentId());
    }
}
